package com.tbsoft.ehr.service.user;

import java.util.List;

import com.tbsoft.ehr.dao.user.UserDao;
import com.tbsoft.ehr.login.RoleInfo;
import com.tbsoft.ehr.login.UserInfo;
import com.tbsoft.ehr.util.ConfigProps;
/**
 * @desc 用戶管理 service
 * @author vidy
 *
 */
public class UserServiceImpl implements UserService {
	private UserDao userDao;
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@Override
	public int getUserCount(UserInfo user) {
		return userDao.getUserCount(user);
	}

	@Override
	public List<UserInfo> getUserList(UserInfo user, int startIndex, int count) {
		return userDao.getUserList(user, startIndex, count);
	}
	
	@Override
	public void saveUser(UserInfo user,List<String> roleIdList) {
		String dePas = userDao.getDecodePassword(user.getUserId());
		user.setPassword(dePas);
		List<RoleInfo> list = userDao.getRoleListByIdList(roleIdList);
		user.getRoleSet().clear();
		if(list != null){
			user.getRoleSet().addAll(list);
		}
		userDao.save(user);
	}

	@Override
	public UserInfo getUser(String userId) {
		return userDao.getUser(userId);
	}

	@Override
	public void updateUserPwd(UserInfo user) {
		userDao.saveOrUpdate(user);
	}
	
	public String getDecodePassword(String pwd){
		return this.userDao.getDecodePassword(pwd);
	}
	@Override
	public void deleteUser(UserInfo user) {
		userDao.delete(user);
	}
	@Override
	public List<RoleInfo> getAllRoleList() {
		return userDao.getAllRoleList();
	}
	@Override
	public void saveUserRole(String userId, List<String> roleIdList) {
		UserInfo usr = userDao.getUser(userId);
		List<RoleInfo> list = userDao.getRoleListByIdList(roleIdList);
		usr.getRoleSet().clear();
		if(list != null){
			usr.getRoleSet().addAll(list);
		}
		userDao.save(usr);
	}
}
